package com.loong.musicdemo.ui.find.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.loong.musicdemo.R;
import com.loong.musicdemo.model.Song;

import java.util.List;

/**
 * 发现部分中的每日新歌曲的adapter
 */
public class DailyNewSongAdapter extends RecyclerView.Adapter<DailyNewSongAdapter.ViewHolder> {
    private List<Song> songList;
    private Context context;
    private RequestOptions requestOptions;

    public DailyNewSongAdapter(List<Song> songList, Context context) {
        this.songList = songList;
        this.context = context;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_recommend_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        requestOptions = new RequestOptions();
        requestOptions.circleCropTransform();
        requestOptions.placeholder(R.drawable.temp);
        requestOptions.bitmapTransform( new RoundedCorners(8));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Song song = songList.get(position);
        holder.m_name.setText(song.getName());
        Glide.with(context)
                .load(song.getPicUrl())
                .apply(requestOptions)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .into(holder.m_img);
        holder.m_singer.setText(song.getSingerName());
    }


    @Override
    public int getItemCount() {
        return songList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView m_img;
        TextView m_name;
        TextView m_singer;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            m_img  = itemView.findViewById(R.id.m_img);
            m_name = itemView.findViewById(R.id.m_name);
            m_singer = itemView.findViewById(R.id.m_singer);
        }
    }
}
